<template>
    <add-contact-dialog :visible="addDialogVisible"
                        :info="supporterInfo"
                        :is-edit="isEditSupporter"
                        :enableCustomer="false"
                        :idSupport="form.idSupport" @callClosed="handleAddDialogClosed"
                        @callSaveSuccess="handleAddSuccess"/>
    <el-dialog :title="dialogTitle" align-center v-model="dialogVisible" width="50%" :before-close="handleClosed">
        <el-form :model="form" ref="form" label-width="100px">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="官方中文名称" prop="name">
                        <el-input v-model="form.nameZh" placeholder="请输入名称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="简称" prop="nameShort">
                        <el-input v-model="form.nameShort" placeholder="请输入简称"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="英文名称" prop="nameEn">
                        <el-input v-model="form.nameEn" placeholder="请输入英文名称"></el-input>
                    </el-form-item>
                </el-col>

            </el-row>
            <el-row justify="space-between">
                <el-col :span="10">
                    <el-form-item label="供应商logo" prop="logo">
                        <el-upload
                                class="avatar-uploader"
                                :action="uploadAPI.uploadImage()"
                                name="file"
                                :auto-upload="true"
                                :show-file-list="false"
                                :on-success="handleAvatarSuccess"
                                :before-upload="beforeAvatarUpload">
                            <img v-if="form.logo" :src="form.logo" class="avatar" :alt="form.logo">
                            <!--                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
                        </el-upload>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="备注" prop="comments">
                        <el-input v-model="form.comments" placeholder="请输入备注"></el-input>
                    </el-form-item>
                    <el-form-item label="评分" prop="score">
                        <el-rate v-model="form.score" show-score></el-rate>
                    </el-form-item>
                    <el-form-item label="用户级别" prop="level">
                        <el-select v-model="form.level" placeholder="请选择用户级别">
                            <el-option label="重点关注" :value="1"></el-option>
                            <el-option label="普通" :value="0"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="官网地址" prop="url">
                <el-input v-model="form.url" placeholder="请输入官网地址">
                    <template #prepend>Http://</template>
                </el-input>
            </el-form-item>
            <el-row>
                <el-col :span="10">
                    <el-form-item label="地址" prop="address">
                        <el-input v-model="form.address" placeholder="请输入地址"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="14">
                    <el-form-item label="统一社会信用代码" prop="socialCode" label-width="200">
                        <el-input v-model="form.socialCode" placeholder="请输入统一社会信用代码"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="供应商简介" prop="profile">
                <el-input type="textarea" v-model="form.profile" placeholder="请输入供应商简介"></el-input>
            </el-form-item>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="form.phone" placeholder="请输入电话"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮箱地址" prop="email">
                        <el-input type="email" v-model="form.email" placeholder="请输入邮箱地址"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <template v-if="form.idSupport !== undefined">
                <el-divider/>
                <el-row justify="space-between">
                    <el-col :span="12">
                        <el-form-item label="联系人" prop="name">
                            <el-input v-model="queryData.name" placeholder="请输入姓名"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10">
                        <el-button-group>
                            <el-button type="primary" @click="hanleQuery">搜索</el-button>
                            <el-button type="primary" @click="handleAddContact" plain>添加</el-button>
                            <el-button type="danger" :disabled="seleted.length === 0" @click="handleDelete" plain>删除
                            </el-button>

                        </el-button-group>
                    </el-col>
                </el-row>
                <el-table :data="contact" height="250" @selectionChange="handleSelectChanged">
                    <el-table-column type="selection"/>
                    <el-table-column prop="name" label="称呼"></el-table-column>
                    <el-table-column prop="phone" label="电话"></el-table-column>
                    <el-table-column prop="email" label="邮箱"></el-table-column>
                    <el-table-column prop="comments" label="备注"></el-table-column>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-link type="primary" @click="handleEdit(scope.row)">编辑</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </template>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: center">
            <el-button @click="handleClosed">取消</el-button>
            <el-button type="primary" @click="submitForm">保存</el-button>
        </div>
    </el-dialog>
</template>

<script>
import supporterAPI from "@/util/request-md/supporter/supporter.js";
import contactAPI from "@/util/request-md/contact/contact.js";
import AddContactDialog from "@/components/widget/dialog/AddContactDialog.vue";
import uploadAPI from "@/util/request-md/file/upload.js";

export default {
    computed: {
        uploadAPI() {
            return uploadAPI
        }
    },
    components: {AddContactDialog},
    emits: ['callClosed', 'callSaveSuccess'],
    props: ['visible', 'dialogTitle', 'currentSupporter'],
    data() {
        return {
            dialogVisible: false,
            isEditSupporter:false,
            addDialogVisible: false,
            form: {
                idSupport: -1,
                name: '',
                url: '',
                socialCode: '',
                status: '',
                logo: '',
                comments: '',
                score: 0,
                level: '',
                address: '',
                nameShort: '',
                nameEn: '',
                profile: '',
                nameZh: '',
                phone: '',
                email: ''
            },
            queryData: {
                name: '',
            },
            supporterInfo:{},
            contact: [{
                name: '张三',
                phone: '1001',
                email: '@as1f5.com'
            }],
            seleted: [],
        };
    },
    watch: {
        'visible'(val) {
            this.dialogVisible = val;
        },
        'currentSupporter'(val) {
            //如果val有值，那么就更新form的值
            if (val.idSupport !== undefined){
                this.form = val;
                this.loadContacts();
            }else{
                this.form ={};
            }

        }
    },
    methods: {
        handleAvatarSuccess(res, file) {
            this.form.logo = res.data;
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isPNG = file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG/PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return (isJPG || isPNG) && isLt2M;
        },
        submitForm() {
            if (this.dialogTitle === '编辑供应商') {
                supporterAPI.updateSupporter(this.form.idSupport, this.form, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '保存成功!'
                        });
                        this.$emit('callSaveSuccess');
                    } else {
                        this.$message.error("保存异常" + res.code);
                        this.$emit('callClosed');
                    }
                })
            }
            if (this.dialogTitle === '添加供应商') {
                supporterAPI.addSupporter(this.form, (res) => {
                    if (res.code === 200) {
                        this.$message({
                            type: 'success',
                            message: '保存成功!'
                        });
                    } else {
                        this.$message.error("保存异常" + res.code);
                    }
                })
            }
        },
        handleAddDialogClosed() {
            this.addDialogVisible = false;
        },
        handleAddSuccess() {
            this.addDialogVisible = false;
            this.loadContacts();
        },
        //删除
        handleDelete() {
            var idContact = [];
            for (let i = 0; i < this.seleted.length; i++) {
                idContact.push(this.seleted[i].idContact);
            }
            console.log(idContact, ">>>>>ids")
            contactAPI.deleteContact({idSupport: this.form.idSupport, idContact: idContact}, (res) => {
                if (res.code === 200) {
                    this.$message.success("删除成功");
                    this.loadContacts();
                } else {
                    this.$message.error("删除失败:" + res.code);
                }
            })
        },
        //查询联系人
        hanleQuery() {
            contactAPI.getContact({idSupport: this.form.idSupport, name: this.queryData.name}, (res) => {
                if (res.code === 200) {
                    this.contact = res.data;
                    this.$message.success("查询成功");
                }
            })
        },
        handleSelectChanged(data) {
            this.seleted = data;
        },
        handleClosed() {
            this.queryData = {
                name: '',
            }
            this.$emit('callClosed');
        },
        //加载供应商联系人信息
        loadContacts() {
            contactAPI.getContact({idSupport: this.form.idSupport, name: this.queryData.name}, (res) => {
                if (res.code === 200) {
                    this.contact = res.data;
                    console.log(this.contact)
                }
            })
        },
        //调出添加联系人的对话框
        handleAddContact() {
            this.addDialogVisible = true;
        },
        handleEdit(data){
            this.isEditSupporter = true;
            this.supporterInfo = data;
            this.addDialogVisible = true;
        }
    },
    mounted() {
        this.loadContacts();
    }
}
</script>

<style>
.avatar-uploader {
    display: inline-block;
    text-align: center;
    width: 120px;
    height: 120px;
    line-height: 120px;
    border: 1px dashed #d9d9d9;
    border-radius: 4px;
    cursor: pointer;

}

.el-upload-dragger {
    padding: 0px;
}

.avatar {
    width: 120px;
    height: 120px;
    display: block;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
}
</style>
